<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

/**
 * Description of model_transaksi
 *
 * @author Hadi
 */
class model_transaksi_internal extends Default_My_Model {

    function __construct() {
        parent::__construct();
    }

    function getSupplierList() {
        return $this->db->query("SELECT id_supplier,nama_supplier FROM master_supplier");
    }

    function getTermConditionList() {
        return $this->db->query("SELECT id_tc, nama_tc, isi_tc FROM master_term_condition");
    }

    public function getTC($kode) {
        return $this->db->query("SELECT id_tc, isi_tc FROM master_term_condition WHERE id_tc = '$kode'" );
    }

    public function getPrefixPOSuppNo() {
        $last_id =  $this->get_scalar("SELECT max(id_po) as id FROM data_po_to_supplier ", "id");
        $number = $last_id+1;
        $prefix = str_pad($number, 5,0,STR_PAD_LEFT);
        return $prefix;
    }

    function getPOSuppData($whr, $offset = 0, $length= 30) {
        $sql = "SELECT q.*, c.*, t.isi_tc, p.nama_port, v.nama_vessel,v.bendera_vessel, v.gt_vessel
                FROM data_po_to_supplier q
                LEFT JOIN master_supplier c ON q.id_supplier = c.id_supplier
                LEFT JOIN master_term_condition t ON t.id_tc = q.id_tc
                LEFT JOIN master_port p ON q.id_port = p.id_port
                LEFT JOIN master_vessel v ON q.id_vessel = v.id_vessel
                $whr LIMIT $length OFFSET $offset";
        return $this->db->query($sql);
    }

    function deletePOSupp($kode) {
        $this->db->delete("data_po_to_supplier", "id_po = '$kode'");
    }

    function getInvoiceSuppData($whr, $offset = 0, $length= 30) {
        $sql = "SELECT p.*, c.*
                FROM data_invoice_from_supplier p
                LEFT JOIN master_supplier c ON p.id_supplier = c.id_supplier
                $whr LIMIT $length OFFSET $offset";

        return $this->db->query($sql);
    }

    function deleteInvoiceSupp($kode) {
        $this->db->delete("data_invoice_from_supplier", "id_invoice = '$kode'");
    }

    function insertSupplier($data) {
        $id = $this->supplierExist($data["nama_supplier"]);
        if ($id == "") {
            $kontak = explode("/", $data["kontak_komplit"]);
            $alamat = explode("\n", $data["alamat_komplit"]);
            $set = array(
                    "nama_supplier" => $data["nama_supplier"],
                    "alamat_detail_supplier" => $data["alamat_detail_supplier"],
                    "attendance" => $data["attendance"] ? $data["attendance"] : "NULL",
                    "telepon_supplier" => $kontak[0],
                    "fax_supplier" => $kontak[1],
                    "email_supplier" => $kontak[2],
                    "alamat_supplier" => $alamat[0],
                    "kota_supplier" => $alamat[1],
                    "kode_pos_supplier" => $alamat[2]
            );
            $this->db->insert("master_supplier", $set);
            return $this->db->insert_id();
        }
        return $id;
    }

    function insertPOSupp($data) {
        $set = array(
                "urgensi" => $data["urgensi"],
                "pic_perusahaan" => str_replace("\n", "<br>", $data["pic_perusahaan"]),
                "tanggal_po" => $data["tanggal_po"],
                "nomor_po" => $data["nomor_po"],
                "rf_number" => $data["rf_number"],
                "id_supplier" => $data["id_supplier"],
                "id_tc" => $data["termcondition"],
                "using_dollar_currency" => $data["using_dollar_currency"],
                "approved_by" => $data["approved_by"],
                "id_port" => $data["id_port"],
                "id_vessel" => $data["id_vessel"],
                "quote_by" => $data["quote_by"],
                "date_submit" => $data["date_submit"],
                "subject" => $data["subject"],
                "submitter" => $data["submitter"],
                "using_ppn" => $data["ppn"],
                "diskon" => $data["diskon"]
        );
        $this->db->insert("data_po_to_supplier", $set);
        return $this->db->insert_id();
    }

    function updatePOSupp($data, $id_po) {
        $set = array(
                "urgensi" => $data["urgensi"],
                "pic_perusahaan" => str_replace("\n", "<br>", $data["pic_perusahaan"]),
                "tanggal_po" => $data["tanggal_po"],
                "nomor_po" => $data["nomor_po"],
                "rf_number" => $data["rf_number"],
                "id_supplier" => $data["id_supplier"],
                "id_tc" => $data["termcondition"],
                "using_dollar_currency" => $data["using_dollar_currency"],
                "approved_by" => $data["approved_by"],
                "quote_by" => $data["quote_by"],
                "date_submit" => $data["date_submit"],
                "subject" => $data["subject"],
                "id_port" => $data["id_port"],
                "id_vessel" => $data["id_vessel"],
                "submitter" => $data["submitter"],
                "using_ppn" => $data["ppn"],
                "diskon" => $data["diskon"]
        );
        $this->db->update("data_po_to_supplier", $set, "id_po = '$id_po'");
    }

    function getItem($id) {
        return $this->get_object("SELECT * FROM master_item WHERE id_item  = '$id'");
    }

    function insertPOSuppItem($set) {
        $this->db->insert("data_po_to_supplier_item", $set);
    }

    function getPOSuppItem($id) {
        return $this->db->query("SELECT * FROM data_po_to_supplier_item WHERE id_po = '$id'");
    }

    function getInvoiceSuppItem($id) {
        return $this->db->query("SELECT * FROM data_invoice_from_supplier_item WHERE id_invoice = '$id'");
    }

    function insertInvoiceSupp($data) {
        $set = array(
                "tanggal_invoice" => $data["tanggal_invoice"],
                "nomor_invoice" => $data["nomor_invoice"],
                "list_po_id" => $data["list_po_id"],
                "id_supplier" => $data["id_supplier"],
                "using_dollar_currency" => $data["using_dollar_currency"],
                "catatan_invoice" => $data["catatan_invoice"],
                "date_submit" => $data["date_submit"],
                "submitter" => $data["submitter"],
                "diskon" => $data["diskon"]
        );
        if(!empty($data["file_upload"]))
            $set["file_attachment_invoice"] = $data["file_upload"];

        $this->db->insert("data_invoice_from_supplier", $set);
        return $this->db->insert_id();
    }

    function updateInvoiceSupp($data, $id_invoice) {
        $set = array(
                "tanggal_invoice" => $data["tanggal_invoice"],
                "nomor_invoice" => $data["nomor_invoice"],
                "list_po_id" => $data["list_po_id"],
                "id_supplier" => $data["id_supplier"],
                "using_dollar_currency" => $data["using_dollar_currency"],
                "catatan_invoice" => $data["catatan_invoice"],
                "date_submit" => $data["date_submit"],
                "submitter" => $data["submitter"],
                "diskon" => $data["diskon"]
        );
        if(!empty($data["file_upload"]))
            $set["file_attachment_invoice"] = $data["file_upload"];
        $this->db->update("data_invoice_from_supplier", $set, "id_invoice = '$id_invoice'");
    }

    function insertInvoiceSuppItem($id_po, $id_item, $qty) {
        $data_item = $this->getItem($id_item);
        $set = array(
                "id_invoice" => $id_po,
                "id_item" => $id_item,
                "nama_item" => $data_item->nama_item,
                "satuan_beli" => $data_item->satuan_beli,
                "quantity" => $qty,
                "harga_beli" => $data_item->harga_beli,
                "harga_jual" => $data_item->harga_jual,
                "harga_beli_dollar" => $data_item->harga_beli_dollar,
                "harga_jual_dollar" => $data_item->harga_jual_dollar
        );
        $this->db->insert("data_invoice_from_supplier_item", $set);
    }

    function supplierExist($nama_pelanggan) {
        $id_customer = $this->get_scalar("SELECT id_supplier FROM master_supplier WHERE nama_supplier = '".  addslashes($nama_pelanggan)."'", "id_supplier");
        return $id_customer;
    }

    function disabledFinalisasi($id) {
        $set = array(
                "is_final" => 1
        );
        $this->db->update("data_invoice_from_supplier", $set, "id_invoice = ".$id);
    }

    function getItemInvoice($idInvoice) {
        $sql = "select * from data_invoice_from_supplier_item where id_invoice=?";
        $resultSet = $this->db->query($sql,array($idInvoice));
        return $resultSet;
    }

}

?>
